package yxy.game.pm2.command.pack;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.PackageHandle;

@MessageType(proto = Command.Package, classtype = PackageMessage.PackageRequest.class)
public class CMD_Package extends Command<PackageMessage.PackageRequest> {

	@Override
	public void execute(MessageSource messageSource, PackageMessage.PackageRequest request) {
		String playerCode = messageSource.getTag();
		int type = request.getType();
		PackageMessage.PackageResponse.Builder response = PackageMessage.PackageResponse.newBuilder();

		CurrencyType currencyType = CurrencyType.forNumber(type);
		if (currencyType == null) {
			send(messageSource, response, Result.ParamError);
			return;
		}
		try {
			PackageMessage.Package.Builder pack = new PackageHandle().getPackage(playerCode, currencyType);
			response.setPack(pack);
			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}

	}

	private void send(MessageSource messageSource, PackageMessage.PackageResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
